package com.oceansoft.mobile.econsole.modules.weixin.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.oceansoft.mobile.econsole.common.constant.Constant;
import com.oceansoft.mobile.econsole.common.util.HttpClientUtil;
import com.oceansoft.mobile.econsole.modules.base.controller.BaseController;
import com.oceansoft.mobile.econsole.modules.base.entity.ApiPager;
import com.oceansoft.mobile.econsole.modules.base.entity.Pager;
import com.oceansoft.mobile.econsole.modules.base.entity.Query;
import com.oceansoft.mobile.econsole.modules.situation.entity.Situation;
import com.oceansoft.mobile.econsole.modules.situation.service.ISituationService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;

/**
 * 高速公路播报
 * Created by smc on 2016/2/16.
 */
@Controller
@RequestMapping("api/highspeed")
public class HighSpeedController extends BaseController{


    /**
     * H5高速路况首页接口
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(value = "index",method = RequestMethod.GET)
    public String gotoHighwayIndex(HttpServletRequest request, Model model){
        String pageindex = request.getParameter("pageindex");
        String res= null;
        try {
            res = HttpClientUtil.doGet(configInfo.getServiceInterfaceAddress()+"/apphub/traffic?ps=10&pi="+pageindex);
        } catch (IOException e) {
            e.printStackTrace();
        }
        JSONObject jsonObject= JSON.parseObject(res);
        ApiPager apiPager = JSON.parseObject(jsonObject.getString("data"), ApiPager.class);
        model.addAttribute("totalpage",apiPager.getTotalPage());
        if(apiPager.getList().size()>0){
            model.addAttribute("highwayNameList",apiPager.getList());
        }else{
            model.addAttribute("highwayNameList","0");
        }
        return "weixin/highspeed/highspeed_road";
    }

    /**
     * H5高速路况加载分页
     *
     * @param request
     * @param model
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "list",method = RequestMethod.GET)
    public ApiPager highwaySituationPager(HttpServletRequest request, Model model){
        String pageindex = request.getParameter("pageindex");

        String  res = null;
        try {
            res = HttpClientUtil.doGet(configInfo.getServiceInterfaceAddress()+"/apphub/traffic?ps=10&pi="+pageindex);
        } catch (IOException e) {
            e.printStackTrace();
        }
        JSONObject jsonObject= JSON.parseObject(res);
        ApiPager apiPager = JSON.parseObject(jsonObject.getString("data"), ApiPager.class);

        return apiPager;
    }

}
